import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
import Quick.SystemComponent.ScreenDevice 0.1
import QtQuick.Controls.Material 2.12
import Quick.SystemComponent.Theme 0.1
import Quick.SystemComponent.FontLoader 0.1
import QtQuick.Controls.Material 2.12
import UI 1.0
import Quick.UIComponent.Dialog.PageDialog 0.1
import Quick.UIComponent.Text.Normal  0.1
import QtQuick.Controls.Universal 2.12
import Qt.labs.platform 1.1
import Qt.labs.settings 1.0
import QtQuick.Layouts 1.12


ApplicationWindow {
    id:root
    visible: true
    width: 886
    height: 600
    title: qsTr("Hello World")
    flags:  Qt.Window | Qt.FramelessWindowHint


    readonly property var styles:["night","light","darkGreen"]

    readonly property var _styles:["夜间模式","日间模式","深蓝模式"]


    StackView{
        id:stack
        anchors.fill: parent
        initialItem: Control{}
    }


    Settings {
        id: settings
        //category: "Style"
        property string style: "Default"
        //property string language:"zh_CN"
    }

    //选择皮肤
    PageDialog{
        id:styleDialog
        dim:true
        width: 320
        height: 280
        title: qsTr("选择皮肤")
        contentItem: ColumnLayout {
            id: settingsColumn
            spacing: 20

            RowLayout {
                Layout.fillWidth: true
                Layout.preferredHeight: settingsColumn.height/2
                spacing: 10
                Label {
                    text: qsTr("选择引擎")
                }

                ComboBox {
                    id: styleBox
                    property int styleIndex: -1
                    model: availableStyles
                    contentItem: MText{
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text:styleBox.currentText
                        pxSize: 20
                        textColor:Theme.textColor
                        fontFamily: Family.normal.name
                    }
                    Component.onCompleted: {
                        styleIndex = find(settings.style, Qt.MatchFixedString)
                        if (styleIndex !== -1)
                            currentIndex = styleIndex
                    }
                    Layout.fillWidth: true
                }
            }

            RowLayout {
                Layout.fillWidth: true
                Layout.fillHeight: true
                spacing: 10
                Label {
                    text: qsTr("选择主题")
                }

                ComboBox{
                    id:_comboBox
                    //flat:true
                    Layout.fillWidth: true
                    contentItem: MText{
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text:_comboBox.currentText
                        pxSize: 20
                        textColor:Theme.textColor
                        fontFamily: Family.normal.name
                    }
                    model: _styles
                }
            }

            Label {
                text: "Restart required"
                color: "#e41e25"
                opacity: (styleBox.currentIndex !== styleBox.styleIndex ) ? 1.0 : 0.0
                horizontalAlignment: Label.AlignHCenter
                verticalAlignment: Label.AlignVCenter
                Layout.fillWidth: true
                Layout.fillHeight: true
            }


        }

        anchors.centerIn: parent
        onOkClicked: {
            settings.style = styleBox.displayText
            setStyle(_comboBox.currentIndex)
        }
    }


    /*! 系统托盘事件 !*/
    SystemTrayIcon{
        id:sysIcon

        //visible: Global.systemTray
        visible:true
        tooltip:qsTr("shanghai  Wavecreast Lo.to")
        iconSource: "qrc:/image/icons/menu.svg"
        onActivated: {
            if(reason===SystemTrayIcon.DoubleClick){
                reshowWindow()
            }
            if(reason===SystemTrayIcon.Context){
                sysIcon.menu.close()
            }
        }
        menu:Menu {

            MenuItem {
                text: qsTr("Open")
                iconSource: "qrc:/image/icons/open.svg"
                onTriggered: reshowWindow()
            }
            MenuItem {
                text: qsTr("Hide")
                iconSource: "qrc:/image/icons/hide.svg"
                onTriggered: root.hide()
            }
            MenuItem {
                text: qsTr("Quit")
                iconSource: "qrc:/image/icons/Quit.svg"
                onTriggered: {
                    sysIcon.hide()
                    Qt.quit()
                }
            }
        }
    }

    onFocusObjectChanged: {
        sysIcon.menu.close()
    }
    onClosing: {
        sysIcon.hide()
        Qt.quit()
    }


    /*!重新打开窗口!*/
    function reshowWindow(){
        root.show()
        root.raise()
        root.requestActivate()
    }


    function updateStyle(){
        Material.primary = Theme.primary
        Material.foreground = Theme.foreground
        Material.background = Theme._background
        Universal.primary = Theme.primary
        Universal.foreground = Theme.foreground
        Universal.background = Theme._background
    }


    function setStyle(index){
        Theme.style =  styles[index]
        updateStyle()
    }


    function openStyleDialog(){
        styleDialog.open()
    }



    Component.onCompleted: {
        setStyle(0)
    }
}
